import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'

const request = axios.create({
	timeout: 10000,
	timeoutErrorMessage: '连接超时....'
})

request.interceptors.request.use((config) => {
	if (config.headers)
		config.headers.token = localStorage.getItem('token') || ''
	return config
}, (error) => {
	return Promise.reject(error)
})


request.interceptors.response.use((response) => {
	const { code } = response.data;
	// 前后端约定的状态码
	switch(code) {
		case 401: 
			console.log('token 非法');
			break;
		case 403:
			console.log('token 失效');		
	}
	return response
}, (error) => {
	const { status } = error.response
	// http 状态码
	switch(status) {
		case 404:
			console.log('资源未找到');
			break
		case 500:
			console.log('服务器错误');	
	}

	return Promise.reject(error)
})


export default request